'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/user/kh/taskCenter 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		const db = uniCloud.database();

		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid,
			type,
			signin_reward,
			sign_datas
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		
		//获取今日0时13位时间戳，用于判断签到数据
		var time = vk.pubfn.getCommonTime(new Date());
		var todayStart = time.todayStart;

		// 业务逻辑开始-----------------------------------------------------------
		if (type == "qiandao") {
			console.log("signin_reward=", signin_reward);
			//如果没传签到奖励金额，则默认30
			if (!signin_reward) {
				signin_reward = 30
			}
			
			let check_signCount = await db.collection('a_sign').where({
				user_id: userInfo._id,
				_add_time: _.gte(todayStart)
			}).count()
			
			if (check_signCount.total > 0) {
				res.code = -1;
				res.msg = "今日已签到！";
				return res;
			}
			
			//添加金豆收支明细
			const addBeanDetail_qiandao = await db.collection('a_jindou_detail').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname || "匿名用户",
				change_num: signin_reward, //扣除的金豆数量
				change_reason: "签到奖励" + signin_reward + "金豆",
				type: "income", //类型：expend:支出,income:收入。
				os_name: originalParam.context.osName || "未知", //用户当时的系统
				appid: userInfo.appid,
				appname: userInfo.appname,
				tfid: userInfo.tfid,
				invite_code: userInfo.invite_code,
				middleman_id: userInfo.middleman_id,
				province: userInfo.province, //省份
				city: userInfo.city, //市
				_add_time: new Date().getTime()
			})
			//添加签到记录
			const addsign_qiandao = await db.collection('a_sign').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname,
				reward: "金豆",
				reward_content: signin_reward + "金豆",
				reward_num: signin_reward,
				os_name: originalParam.context.osName || "未知", //用户当时的系统
				appid: userInfo.appid,
				appname: userInfo.appname,
				tfid: userInfo.tfid,
				invite_code: userInfo.invite_code,
				middleman_id: userInfo.middleman_id,
				province: userInfo.province, //省份
				city: userInfo.city, //市
				sign_datas: sign_datas + 1, //连续签到天数
				_add_time: new Date().getTime()
			})

			//给用户增加金豆
			const updateUser_qiandao = await db.collection('uni-id-users').where({
				_id: userInfo._id
			}).update({
				balance: _.inc(signin_reward),
				finish: {
					xinren: true
				}
			})
		}

		if (type == "libao") {
			const recharge = await db.collection('a_recharge').where({
				user_id: userInfo._id,
				pay_status: 1
			}).count()
			console.log("recharge", recharge);
			if (recharge.total > 0) {
				//添加金豆收支明细
				const addBeanDetail_libao = await db.collection('a_jindou_detail').add({
					user_id: userInfo._id,
					nickname: userInfo.nickname,
					change_num: 50, //变化的金豆数量
					change_reason: "首充奖励50金豆",
					type: "income", //类型：expend:支出,income:收入。
					os_name: originalParam.context.osName || "未知", //用户当时的系统
					appid: userInfo.appid,
					appname: userInfo.appname,
					tfid: userInfo.tfid,
					invite_code: userInfo.invite_code,
					middleman_id: userInfo.middleman_id,
					province: userInfo.province, //省份
					city: userInfo.city, //市
					_add_time: new Date().getTime()
				})
				//给用户增加金豆
				const updateUser_libao = await db.collection('uni-id-users').where({
					_id: userInfo._id
				}).update({
					finish: {
						libao: true
					},
					balance: _.inc(50)
				})
				res.recharge = recharge.total
			} else {
				res.recharge = 0
			}
		}

		if (type == "jiesuo") {
			//添加金豆收支明细
			const addBeanDetail_jiesuo = await db.collection('a_jindou_detail').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname,
				change_num: 50, //变化的金豆数量
				change_reason: "解锁智能推荐奖励50金豆",
				type: "income", //类型：expend:支出,income:收入。
				os_name: originalParam.context.osName || "未知", //用户当时的系统
				appid: userInfo.appid,
				appname: userInfo.appname,
				tfid: userInfo.tfid,
				invite_code: userInfo.invite_code,
				middleman_id: userInfo.middleman_id,
				province: userInfo.province, //省份
				city: userInfo.city, //市
				_add_time: new Date().getTime()
			})
			//给用户增加金豆
			const updateUser_jiesuo = await db.collection('uni-id-users').where({
				_id: userInfo._id
			}).update({
				finish: {
					jiesuo: true
				},
				balance: _.inc(50)
			})
			res.jiangli = 50
		}


		if (type == "xinren") {
			//添加金豆收支明细
			const addBeanDetail_xinren = await db.collection('a_jindou_detail').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname,
				change_num: 30, //变化的金豆数量
				change_reason: "新人奖励30金豆",
				type: "income", //类型：expend:支出,income:收入。
				os_name: originalParam.context.osName || "未知", //用户当时的系统
				appid: userInfo.appid,
				appname: userInfo.appname,
				tfid: userInfo.tfid,
				invite_code: userInfo.invite_code,
				middleman_id: userInfo.middleman_id,
				province: userInfo.province, //省份
				city: userInfo.city, //市
				_add_time: new Date().getTime()
			})
			//给用户增加金豆
			const updateUser_xinren = await db.collection('uni-id-users').where({
				_id: userInfo._id
			}).update({
				finish: {
					xinren: true
				},
				balance: _.inc(30)
			})

			res.jiangli = 30
		}

		if (type == "shoujihao") {
			//添加金豆收支明细
			const addBeanDetail_shoujihao = await db.collection('a_jindou_detail').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname,
				change_num: 30, //变化的金豆数量
				change_reason: "完善手机号奖励30金豆",
				type: "income", //类型：expend:支出,income:收入。
				os_name: originalParam.context.osName || "未知", //用户当时的系统
				appid: userInfo.appid,
				appname: userInfo.appname,
				tfid: userInfo.tfid,
				invite_code: userInfo.invite_code,
				middleman_id: userInfo.middleman_id,
				province: userInfo.province, //省份
				city: userInfo.city, //市
				_add_time: new Date().getTime()
			})
			//给用户增加金豆
			const updateUser_shoujihao = await db.collection('uni-id-users').where({
				_id: userInfo._id
			}).update({
				finish: {
					shoujihao: true
				},
				balance: _.inc(30),
				mobile: data.mobile
			})
			res.jiangli = 30
		}

		if (type == "dingyue") {
			//添加金豆收支明细
			const addBeanDetail_dingyue = await db.collection('a_jindou_detail').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname,
				change_num: 15, //变化的金豆数量
				change_reason: "订阅消息奖励15金豆",
				type: "income", //类型：expend:支出,income:收入。
				os_name: originalParam.context.osName || "未知", //用户当时的系统
				appid: userInfo.appid,
				appname: userInfo.appname,
				tfid: userInfo.tfid,
				invite_code: userInfo.invite_code,
				middleman_id: userInfo.middleman_id,
				province: userInfo.province, //省份
				city: userInfo.city, //市
				_add_time: new Date().getTime()
			})

			//给用户增加金豆
			const updateUser_dingyue = await db.collection('uni-id-users').where({
				_id: userInfo._id
			}).update({
				finish: {
					dingyue: true
				},
				balance: _.inc(15)
			})
			res.jiangli = 15
		}

		if (type == "zhuomian") {
			//此方法腾讯云和阿里云都支持
			let newInfo = await vk.baseDao.updateAndReturn({
				dbName: "uni-id-users",
				whereJson: {
					_id: userInfo._id
				},
				dataJson: {
					finish: {
						zhuomian: true
					},
					balance: _.inc(50)
				}
			});

			//添加金豆收支明细
			db.collection('a_jindou_detail').add({
				user_id: userInfo._id,
				nickname: userInfo.nickname,
				change_num: 50, //变化的金豆数量
				changed_num: newInfo.balance, //支付后金豆余额
				change_reason: "添加到桌面奖励50金豆",
				type: "income", //类型：expend:支出,income:收入。
				os_name: originalParam.context.osName || "未知", //用户当时的系统
				appid: userInfo.appid,
				appname: userInfo.appname,
				tfid: userInfo.tfid,
				invite_code: userInfo.invite_code,
				middleman_id: userInfo.middleman_id,
				province: userInfo.province, //省份
				city: userInfo.city, //市
				_add_time: new Date().getTime()
			})
			res.jiangli = 50
		}



		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
